package com.yihusitian.leecode.algorithm.easy;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

public class RomanToInteger {

    public int romanToInt2(String s) {
        if (Objects.isNull(s) || s.isEmpty()) {
            return 0;
        }
        int result = 0;
        for (ValueMapping valueMapping : ValueMapping.values()) {
            int oldLength = s.length();
            if (oldLength == 0) {
                break;
            }
            String name = valueMapping.name();
            s = s.replaceAll(name, "");
            int newlength = s.length();
            result += ((oldLength - newlength) / (name.length())) * valueMapping.getPervalue();
        }
        return result;
    }


    enum ValueMapping {
        IV(4),
        IX(9),
        XL(40),
        XC(90),
        CD(400),
        CM(900),
        I(1),
        V(5),
        X(10),
        L(50),
        C(100),
        D(500),
        M(1000);
        private int pervalue;

        public int getPervalue() {
            return this.pervalue;
        }

        ValueMapping(int pervalue) {
            this.pervalue = pervalue;
        }
    }



    /**
     * I 可以放在 V (5) 和 X (10) 的左边，来表示 4 和 9。
     * X 可以放在 L (50) 和 C (100) 的左边，来表示 40 和 90。 
     * C 可以放在 D (500) 和 M (1000) 的左边，来表示 400 和 900。
     *
     * @param s
     * @return
     */
    public int romanToInt(String s) {
        if (Objects.isNull(s) || s.isEmpty()) {
            return 0;
        }
        char[] chs = s.toCharArray();
        int result = 0;
        int i = 0;
        while (i < chs.length) {
            char currentChar = chs[i];
            int currentValue = this.getIntValueByRomanChar(currentChar);
            if (i < chs.length - 1) {
                char nextChar = chs[i + 1];
                if (
                        (currentChar == 'I' && (nextChar == 'V' || nextChar == 'X')) ||
                        (currentChar == 'X' && (nextChar == 'L' || nextChar == 'C')) ||
                        (currentChar == 'C' && (nextChar == 'D' || nextChar == 'M'))
                ) {
                    currentValue *= -1;
                }

            }
            result += currentValue;
            i++;
        }
        return result;
    }

    private int getIntValueByRomanChar(char ch) {
        switch (ch) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
        }
        return 0;
    }

    public static void main(String[] args) {
//        System.out.println(new RomanToInteger().romanToInt("III"));
//        System.out.println(new RomanToInteger().romanToInt("IV"));
//        System.out.println(new RomanToInteger().romanToInt("IX"));
//        System.out.println(new RomanToInteger().romanToInt("LVIII"));
        System.out.println(new RomanToInteger().romanToInt("MCMXCIV"));
        System.out.println(new RomanToInteger().romanToInt2("MCMXCIV"));

    }


}
